package com.kxmall.web.controller.count;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kxmall.categorize.domain.HsWasteCategorize;
import com.kxmall.categorize.mapper.HsWasteCategorizeMapper;
import com.kxmall.common.core.domain.R;
import com.kxmall.count.countParam.RecoverParam;
import com.kxmall.count.countVO.LargeScreenVO;
import com.kxmall.count.countVO.RecoveryCountVO;
import com.kxmall.dashboard.service.StatisticsBizService;
import com.kxmall.huishou.domain.HsSettlementOrder;
import com.kxmall.huishou.domain.HsWasteSite;
import com.kxmall.huishou.mapper.HsSettlementOrderMapper;
import com.kxmall.huishou.mapper.HsWasteSiteMapper;
import com.kxmall.huishou.service.IHsSettlementOrderService;
import com.kxmall.rider.domain.KxRider;
import com.kxmall.rider.mapper.KxRiderMapper;
import com.kxmall.user.domain.KxUser;
import com.kxmall.user.mapper.KxUserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;


/**
 * 废品分类信息
 *
 * @author kxmall
 * @date 2023-11-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/count/count")
public class CountController {

    private final HsSettlementOrderMapper hsSettlementOrderMapper;   //回收结算订单
    private final IHsSettlementOrderService hsSettlementOrderService;   //回收结算订单
    private final HsWasteCategorizeMapper hsWasteCategorizeMapper;   //废品分类信息
    private final HsWasteSiteMapper hsWasteSiteMapper;   //废品回收站信息
    private final KxRiderMapper kxRiderMapper;   //回收员申请信息
    private final KxUserMapper kxUserMapper;   //用户信息

    private final StatisticsBizService statisticsBizService;

    /**
     * 回收统计*
     */
    @PostMapping("/getRecoveryCount")
    public R<RecoveryCountVO> getRecoveryCount() {
        RecoveryCountVO recoveryCountVO = new RecoveryCountVO();
        // 废品种类数
        Long size = hsWasteCategorizeMapper
                .selectCount(Wrappers.<HsWasteCategorize>lambdaQuery()
                        .ne(HsWasteCategorize::getParentId, 0));
        recoveryCountVO.setRecoveryTypeNum(String.valueOf(size));
        // 今日回收重量 历史回收重量
        BigDecimal weightTalToday = new BigDecimal(0);
        BigDecimal weightTalHistory = new BigDecimal(0);

        LocalDateTime now = LocalDateTime.now();
        // 统计 今日回收重量 历史回收重量
        LambdaQueryWrapper<HsSettlementOrder> q = new LambdaQueryWrapper<>();
        q.eq(HsSettlementOrder::getStatus, 4);
        List<HsSettlementOrder> hsSettlementOrders = hsSettlementOrderMapper.selectList(q);
        for (HsSettlementOrder hsSettlementOrder : hsSettlementOrders) {
            LocalDate localDate = hsSettlementOrder.getCreateTime().toInstant()
                    .atZone(ZoneId.systemDefault())
                    .toLocalDate();
            if (localDate.compareTo(now.toLocalDate()) == 0) {
                weightTalToday = weightTalToday.add(hsSettlementOrder.getWeightTal());
            }
            weightTalHistory = weightTalHistory.add(hsSettlementOrder.getWeightTal());
        }
        recoveryCountVO.setRecoveryWeightToday(String.valueOf(weightTalToday));
        recoveryCountVO.setRecoveryWeightHistory(String.valueOf(weightTalHistory));

        // 回收站数
        Long hsWasteSites = hsWasteSiteMapper
                .selectCount(Wrappers.<HsWasteSite>lambdaQuery().eq(HsWasteSite::getDelFlag, 0L));
        recoveryCountVO.setRecoveryBinNum(String.valueOf(hsWasteSites));

        // 回收员数
        Long kxRiderNum = kxRiderMapper
                .selectCount(Wrappers.<KxRider>lambdaQuery().eq(KxRider::getState, 1));
        recoveryCountVO.setRecoveryPersonNum(String.valueOf(kxRiderNum));

        // 今日订单数 历史订单数
        Long orderToday = hsSettlementOrderMapper
                .selectCount(Wrappers.<HsSettlementOrder>lambdaQuery()
                        .ne(HsSettlementOrder::getStatus, 1)
                        .ge(HsSettlementOrder::getCreateTime, now.toLocalDate()));
        Long orderHistory = hsSettlementOrderMapper
                .selectCount(Wrappers.<HsSettlementOrder>lambdaQuery()
                        .ne(HsSettlementOrder::getStatus, 1));

        recoveryCountVO.setOrderToday(String.valueOf(orderToday));
        recoveryCountVO.setOrderHistory(String.valueOf(orderHistory));

        // 用户总数
        Long kxUsers = kxUserMapper
                .selectCount(Wrappers.<KxUser>lambdaQuery().eq(KxUser::getIsDel, 0));
        recoveryCountVO.setUserCount(String.valueOf(kxUsers));

        // 今日登录用户数 本周登录用户数
        Long userToday = kxUserMapper
                .selectCount(Wrappers.<KxUser>lambdaQuery()
                        .eq(KxUser::getIsDel, 0)
                        .ge(KxUser::getCreateTime, now.toLocalDate()));
        // 获取本周的第一天（默认是周一）
        LocalDate startOfWeek = now.toLocalDate().with(DayOfWeek.MONDAY);
        Long userWeek = kxUserMapper
                .selectCount(Wrappers.<KxUser>lambdaQuery()
                        .eq(KxUser::getIsDel, 0)
                        .ge(KxUser::getCreateTime, startOfWeek));

        recoveryCountVO.setUserToday(String.valueOf(userToday));
        recoveryCountVO.setUserWeek(String.valueOf(userWeek));
        return R.ok(recoveryCountVO);
    }

    /**
     * 回收数据统计*
     */
    @PostMapping("/getRecoveryChartVOList")
    public R<?> getRecoveryChartVOList(@RequestBody RecoverParam recoverParam) {
        return R.ok(hsSettlementOrderService.getRecoveryChartVOList(recoverParam));
    }

    /**
     * 大屏数据统计
     */
    @PostMapping("/getLargeScreen")
    public R<LargeScreenVO> getLargeScreen() {
        return R.ok(statisticsBizService.getScreenData());
    }


}
